// pages/details/details.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    show: false,
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 500,
    detailList: {},
    imagesList: [],
    FileList: [],
    mapList: {},
    showMap: false,
    mapAllList: {},
    checked: true,
    subAdd: 1,
    getDetailList: {},
    imagesDetailList: {},
    addressList: {},
    result: '',
    letbie: [],
    geggId: {},
    ggid: {},
    msg: '',
    collect: 0,
    test: '',
    xqImgUrl: ''
  },
  // 复选框
  onChangeGroup(e) {
    this.setData({
      result: e.detail
    })
    for (let i = 0; i < this.data.mapAllList.length; i++) {
      if (e.detail == this.data.mapAllList[i].id) {
        this.setData({
          mapList: this.data.mapAllList[i]
        })
      }
    }
  },
  // 步进器
  onChange(event) {
    this.setData({
      subAdd: event.detail
    });
    this.clickdemo()
  },
  // 显示选择遮罩层
  showPopup() {
    this.setData({
      show: true
    });
  },
  // 显示地址遮罩层
  onClickShowMap() {
    this.setData({
      showMap: true
    });
  },
  // 隐藏地址遮罩层
  onClickHideMap() {
    this.setData({
      showMap: false
    });
  },
  // 隐藏选择遮罩层
  onClickHide() {
    this.setData({
      show: false
    });
  },
  // 返回顶部按钮
  scrollTop: function () {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  // 分享
  onShareAppMessage: function (ops) {
    if (ops.from === 'button') {}
    return {
      title: `XXX`,
      path: `XXX`,
      success: function (res) {
        // 转发成功

      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  // 去购物车页面
  onClickButton() {
    wx.reLaunch({
      url: '../shoppingCart/shoppingCart'
    })
  },
  clickdemo(e) {
    var that = this
    let key = e.currentTarget.dataset.id
    let value = e.currentTarget.dataset.name
    // this.data.msg += e.currentTarget.dataset.name + ' '
    this.data.geggId[key] = value
    this.data.geggId.specsAttId = e.currentTarget.dataset.url
    // console.log(e.currentTarget.dataset.url)
    this.setData({
      xqImgUrl: 'https://wxmall.banmapc.com:9099/wx/api/v1/getFile?id=' + e.currentTarget.dataset.url
    })
    let jsonStr = JSON.stringify(this.data.geggId)
    wx.request({
      url: app.globalData.baseUrl + '/product/findSpecsPrice',
      data: {
        xcxOpenId: app.globalData.xcxOpenId,
        specsJson: encodeURIComponent(jsonStr),
        productId: this.data.detailList.id
      },
      method: 'get',
      success(res) {
        that.data.ggid = res.data.data
        that.setData({
          ggid: res.data.data,
        })
      }
    });
    let str = ''
    for (let key in this.data.geggId) {
      if (key != 'specsAttId') {
        str += this.data.geggId[key] + ' '
      }
    }

    this.setData({
      test: value,
      msg: str
    })
  },
  toshopCart(e) {
    var that = this
    let data = {}
    if (app.globalData.xcxOpenId == '') {
      wx.showToast({
        title: '请先登录再加入购物车喔~',
        icon: 'none'
      })
      setTimeout(function () {
        wx.reLaunch({
          url: '../login/login',
        })
      }, 1500)
      return
    }
    if (this.data.ggid.id) {
      data = {
        xcxOpenId: app.globalData.xcxOpenId,
        productId: this.data.detailList.id,
        productSpecsId: this.data.ggid.id,
        // priceAfterDiscount: this.data.ggid.currency ? this.data.ggid.currency : this.data.detailList.currency,
        // price: this.data.ggid.amount ? this.data.ggid.amount : this.data.detailList.price,
        num: this.data.subAdd
      }
    } else {
      data = {
        xcxOpenId: app.globalData.xcxOpenId,
        productId: this.data.detailList.id,
        // priceAfterDiscount: this.data.ggid.currency ? this.data.ggid.currency : this.data.detailList.currency,
        // price: this.data.ggid.amount ? this.data.ggid.amount : this.data.detailList.price,
        num: this.data.subAdd
      }
    }
    wx.request({
      url: app.globalData.baseUrl + '/goods_shopcar/addShopCarByUser',
      data: data,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: 'POST',
      success(res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        that.setData({
          show: false
        });
        if (res.data.code == 0) {
          setTimeout(function () {
            wx.reLaunch({
              url: '../shoppingCart/shoppingCart'
            })
          }, 1000)
        }
      }
    });
  },
  onClickIcon() {
    wx.showToast({
      title: '暂未开放，敬请期待',
    })
  },
  toBuy(e) {
    // // console.log(this.data.ggid)
    if (app.globalData.xcxOpenId == '') {
      wx.showToast({
        title: '请先登录再进行购买喔~',
        icon: 'none'
      })
      setTimeout(function () {
        wx.reLaunch({
          url: '../login/login',
        })
      }, 1500)
      return
    }
    var demo = {}
    console.log(this.data.letbie)
    if (this.data.letbie.length == 0) {
      demo = {
        num: this.data.subAdd,
        productId: this.data.detailList.id,
        amount: this.data.detailList.price,
        currency: this.data.detailList.currency
      }
    } else {
      demo = {
        productSpecsId: this.data.ggid.id,
        num: this.data.subAdd,
        productId: this.data.detailList.id,
        amount: this.data.ggid.amount,
        currency: this.data.ggid.currency
      }
    }
    if (JSON.stringify(demo) == "{}") {
      this.showPopup()
      return
    }
    // ggid subAdd detailList
    let obj = {
      orderAmount: this.data.ggid.amount,
      imgUrl: this.data.xqImgUrl,
      subtitle: this.data.detailList.subtitle,
      productList: [
        demo
      ]
    }
    const str = JSON.stringify(obj)
    wx.setStorage({
      data: str,
      key: 'buyStr',
    })

    wx.setStorage({
      data: demo.currency * demo.num,
      key: 'allMoneyzc',
    })
    wx.setStorage({
      data: demo.amount * demo.num,
      key: 'allMoneyprice',
    })
    wx.navigateTo({
      url: '/pages/nowBuy/nowBuy'
    })
  },
  shouchang() {
    if (app.globalData.xcxOpenId == '') {
      wx.showToast({
        title: '请先登录再添加收藏喔~',
        icon: 'none'
      })
      setTimeout(function () {
        wx.reLaunch({
          url: '../login/login',
        })
      }, 1500)
      return
    }
    var that = this
    if (that.data.collect == 1) {
      that.setData({
        collect: 2
      })
    } else {
      that.setData({
        collect: 1
      })
    }
    wx.request({
      url: app.globalData.baseUrl + '/product/collect',
      data: {
        xcxOpenId: app.globalData.xcxOpenId,
        productId: that.data.detailList.id,
        type: that.data.collect
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: 'POST',
      success(res) {
        // // console.log('点击了收藏')
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载detailList
   */
  onLoad: function (options) {
    var that = this
    // 获取商品详情
    wx.request({
      url: app.globalData.baseUrl + '/product/findProductDetail',
      data: {
        xcxOpenId: app.globalData.xcxOpenId,
        // productId: 2,
        productId: options.cId
      },
      method: 'get',
      success(res) {
        // console.log(res.data.data)
        let arr = res.data.data.bannerFileList != null ? res.data.data.bannerFileList.split(",") : []
        let bannerFileList = []
        for (let i = 0; i < arr.length; i++) {
          bannerFileList.push(app.globalData.baseUrl + '/getFile?id=' + arr[i])
        }
        res.data.data.bannerFileList = bannerFileList
        res.data.data.attributeList = JSON.parse(res.data.data.attributeList)

        if (res.data.data.attributeList && res.data.data.attributeList.length > 0) {
          let attributeKeyObj = res.data.data.attributeList[0];
          that.setData({
            letbie: Object.keys(attributeKeyObj)
          })
          let index = that.data.letbie.indexOf("specsAttId");
          if (index > -1) {
            that.data.letbie.splice(index, 1);
          }
          that.setData({
            letbie: that.data.letbie
          })
        }
        that.setData({
          detailList: res.data.data,
          collect: res.data.data.collect,
          xqImgUrl: res.data.data.bannerFileList[0]
        })
        for (let i = 0; i < that.data.letbie.length; i++) {
          that.data.geggId[that.data.letbie[i]] = ''
        }
        that.data.geggId.specsAttId = ''
        let str = res.data.data.detailFileList != null ? res.data.data.detailFileList.split(",") : ''
        let strarr = []
        for (let i = 0; i < str.length; i++) {
          strarr.push(app.globalData.baseUrl + '/getFile?id=' + str[i])
        }
        that.setData({
          FileList: strarr
        })
      }
    });
    // 地址
    wx.request({
      url: app.globalData.baseUrl + '/shipping_address/getAddressByUser',
      data: {
        xcxOpenId: app.globalData.xcxOpenId
      },
      success(res) {
        that.setData({
          mapList: res.data.data
        })
      }
    })
    wx.request({
      url: app.globalData.baseUrl + '/shipping_address/getAddressListByUser',
      data: {
        xcxOpenId: app.globalData.xcxOpenId
      },
      success(res) {
        that.setData({
          mapAllList: res.data.data
        })
      }
    })
  },
  onShow: function () {
    var that = this
    setTimeout(function () {
      wx.request({
        url: app.globalData.baseUrl + '/integral/freedIntegralPackage',
        data: {
          xcxOpenId: app.globalData.xcxOpenId,
          productId: that.data.detailList.id
        },
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success(res) {
          if (res.data.code == 0) {
            wx.showToast({
              title: 'ZC释放成功',
              icon: 'success'
            })
          }
        }
      })
    }, 15000)
  }
})